package cs575.server.services;

import java.nio.ByteBuffer;
import java.util.List;

import cs575.common.FantasyMessage;
import cs575.common.OpCodes;
import cs575.common.Player;
import cs575.server.FantasyServer;

public class GetAllPlayers {

	public FantasyMessage execute(FantasyMessage message) {
		// Size of integer, number of players returned.
		int responseMsgSize = 4;
		List<Player> toReturn = FantasyServer.getAllPlayers();

		for (int i = 0; i < toReturn.size(); i++) {
			// Magic number - 4 is the size on an integer
			responseMsgSize += toReturn.get(i).size() + 4;
		}

		ByteBuffer responseBB = ByteBuffer.allocate(responseMsgSize);
		responseBB.putInt(toReturn.size());

		for (Player p : toReturn) {
			responseBB.putInt(p.size());
			responseBB.put(p.toBytes());
		}

		FantasyMessage response = new FantasyMessage();
		response.setOpCode(OpCodes.GET_ALL_PLAYERS_RESPONSE);
		response.setMessage(responseBB.array());
		return response;

	}
}
